<template>
  <el-container>
    <el-main>
      <div class="default-main ba-table-box">
        <TableHeader
          :buttons="['refresh', 'export', 'comSearch', 'columnDisplay']"
          :quick-search-placeholder="placeholder"
          @action="baTable.onTableHeaderAction"
        />
        <!-- 表格 -->
        <!-- 要使用`el-table`组件原有的属性，直接加在Table标签上即可 -->
        <Table ref="tableRef" @action="baTable.onTableAction" />
      </div>
    </el-main>
  </el-container>
</template>
<script lang="ts" setup>
import { ref, provide, onMounted, markRaw, reactive, inject } from "vue";
import baTableClass from "@/utils/baTable";
import { baTableApi, operatorShowOff, getDictData } from "@/utils/common"; // 导入表格api方法生成器
import Table from "@/components/table/index.vue";
import TableHeader from "@/components/table/header/index.vue";
import useCurrentInstance from "@/utils/useCurrentInstance";
import type { TableRow, TableColumn } from "@/utils/baTable"; //表格列行类型
import { ElectricData } from "@/api/backend/index";
import { useRoute } from "vue-router";
import { useNavTabs } from "@/store/navTabs";
import DateTime from "@/components/datePicker/index.vue";

const placeholder = ref<string>("模糊搜索");
const tableRef = ref();
const route = useRoute();
const navTabs = useNavTabs();
const dictionary = inject("dictionary") as any;
const cabinet = inject("cabinet") as any;
const itemObject = inject("itemObject") as any;
const site = inject("site") as any;

//字段显隐记忆功能
const filedShowOff = (filter: any) => {
  if ((navTabs as any).columnDisplayStoreMap) {
    return (navTabs as any).columnDisplayStoreMap.get(
      route.path + "/" + filter
    );
  }
  return true;
};
// 每页个数记忆功能
const pageSizeKeep = () => {
  if ((navTabs as any).pageSizeMap) {
    return (navTabs as any).pageSizeMap.get(route.path + ElectricData)
      ? (navTabs as any).pageSizeMap.get(route.path + ElectricData)
      : 10;
  }
  return 10;
};

const state: {
  defaultFileds: any[]; //默认内循环温度
} = reactive({
  defaultFileds: [],
});

const showFileds = (val: string) => {
  let tempArr = [];
  let len = itemObject.fullData.length;
  for (let i = 0; i < len; i++) {
    let temObj = {
      show: filedShowOff(""),
      label: "",
      prop: "",
      align: "center",
      operator: false,
    };
    if (itemObject.fullData[i].categoryName == val) {
      temObj.show = filedShowOff(itemObject.fullData[i].mqttFiled);
      // temObj.label = itemObject.fullData[i].objectName;
      temObj.label =
        cabinet.tags.cabinetType[cabinet.single.cabinetType] == "卧式"
          ? itemObject.fullData[i].objectName
          : itemObject.fullData[i].objectNameVertical;
      temObj.prop = itemObject.fullData[i].mqttFiled;
      tempArr.push(temObj);
    }
  }
  state.defaultFileds = tempArr;
};
showFileds(cabinet.history);

const getValue = () => {
  baTable.comSearch.form = Object.assign(baTable.comSearch.form, {
    params: {
      beginTime: cabinet.beginTime,
      endTime: cabinet.endTime,
    },
  });
};

//全局事件总线
const { proxy } = useCurrentInstance();
const baTable = new baTableClass(
  new baTableApi(ElectricData), //this.api = api菜单管理相关
  {
    pk: "id",
    column: [
      { type: "selection", align: "center", operator: false },
      {
        show: filedShowOff("code"),
        label: "网关编号",
        prop: "code",
        align: "center",
        operatorPlaceholder: "精确搜索",
      },
      {
        show: filedShowOff("name"),
        label: "设备名称",
        prop: "name",
        align: "center",
        operatorPlaceholder: "精确搜索",
      },
      {
        show: filedShowOff("heart"),
        label: "心跳",
        prop: "heart",
        align: "center",
        operatorPlaceholder: "精确搜索",
      },
      {
        show: filedShowOff("end"),
        label: "报文结束",
        prop: "end",
        align: "center",
        operatorPlaceholder: "精确搜索",
      },
      ...state.defaultFileds,
      {
        show: filedShowOff("createTime"),
        label: "创建时间",
        prop: "createTime",
        comSearchRender: "customRender",
        comSearchCustomRender: markRaw(DateTime),
      },
      {
        show: false,
        label: "操作",
        align: "center",
        width: "130",
        render: "buttons",
        fixed: "right",
        operator: false,
        buttons: [
          {
            render: "tipButton",
            name: "edit",
            title: "编辑",
            text: "",
            type: "primary",
            icon: "iconfont icon-pencil",
            class: "table-row-edit",
            disabledTip: false,
            // 按钮是否显示，请返回布尔值
            display: (row: TableRow, field: TableColumn) => {
              return false;
            },
            // 按钮是否禁用，请返回布尔值
            disabled: (row: TableRow, field: TableColumn) => {
              return false;
            },
            attr: {},
          },
          {
            render: "tipButton",
            name: "info",
            title: "详情",
            text: "",
            type: "primary",
            icon: "fa fa-search-plus",
            class: "table-row-edit",
            disabledTip: false,
            // 按钮是否显示，请返回布尔值
            display: (row: TableRow, field: TableColumn) => {
              return false;
            },
            // 按钮是否禁用，请返回布尔值
            disabled: (row: TableRow, field: TableColumn) => {
              return false;
            },
            attr: {},
          },
          {
            render: "confirmButton",
            name: "remove",
            title: "删除",
            text: "",
            type: "danger",
            icon: "el-icon-DeleteFilled",
            class: "table-row-delete",
            popconfirm: {
              confirmButtonText: "删除",
              cancelButtonText: "取消",
              confirmButtonType: "danger",
              title: "确定删除所选项吗",
            },
            disabledTip: false,
            // 按钮是否显示，请返回布尔值
            display: (row: TableRow, field: TableColumn) => {
              return false;
            },
            // 按钮是否禁用，请返回布尔值
            disabled: (row: TableRow, field: TableColumn) => {
              return false;
            },
            attr: {},
          },
        ],
      },
    ],
    filter: { cabinetId: route.query.cabinetId, pageSize: pageSizeKeep() },
    acceptQuery: false, //不接受url的query参数并自动触发通用搜索
  },
  {
    // 默认表单数据(添加)
    defaultItems: {
      cabinetType: 0, //立式
    },
  }
);

//获取字典
const getDictionary = () => {};
getDictionary();

onMounted(() => {
  getValue();
  baTable.table.ref = tableRef.value;
  baTable.mount();
  baTable.getList(ElectricData);

  /**
   * 表格内的按钮响应
   * @param name 按钮name
   * @param row 被操作行数据
   */
  proxy.eventBus.on(
    "onTableButtonClick",
    (data: { name: string; row: TableRow }) => {
      if (!baTable.activate) return;
      //回滚功能暂无
      if (data.name == "rollback") {
        // onRollback([data.row[baTable.table.pk!]])
      } else if (data.name == "info") {
        //携带主键字段pk
        //   infoButtonClick(
        //     (data.row as TableRow)[baTable.table.pk!],
        //     data.row as any
        //   );
      }
    }
  );
});

// 实例化表格后，将 baTable 的实例提供给上下文
provide("baTable", baTable);
defineExpose({
  showFileds,
});
</script>
<style lang="scss" scoped>
.el-main {
  padding: 0;
}
.default-main {
  margin: var(--ba-main-space) var(--ba-main-space) 60px var(--ba-main-space);
}
</style>
